package com.docker.sys.permission;

import com.docker.core.mvc.APPID;
import com.docker.core.mvc.Remark;
import com.docker.core.mvc.service.BaseService;
import com.docker.sys.role.rolePerm.RolePermService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Action;
import com.jfinal.core.JFinal;
import com.jfinal.kit.Okv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ldl
 * @Create 2020/09/18 018 15:18
 */
public class PermissionService extends BaseService {

    @Inject
    private PermissionDao permissionDao;

    @Inject
    private RolePermService rolePermService;

    public Page<Record> paginate(Okv cond, int pageNum, int pageSize) {
        return permissionDao.paginate(cond, pageNum, pageSize);
    }

    public boolean tidy() {
        List<String> allActionKeys = JFinal.me().getAllActionKeys();

        List<Record> bindList = rolePermService.bindList();
        //删除以前绑定的，但现在已经不存在的
        for (Record bind : bindList) {
            if (!allActionKeys.contains(bind.getStr("action_key"))) {
                //删除角色关联
                rolePermService.deletePerm(bind.getStr("perm_id"));
                //删除权限
                permissionDao.delete(bind.getStr("perm_id"));
            }
        }

        //删除不存在的
        List<Record> pmsnList = permissionDao.list(null);
        for (Record pmsn : pmsnList) {
            if (!allActionKeys.contains(pmsn.getStr("action_key"))) {
                permissionDao.delete(pmsn.getStr("perm_id"));
            }
        }
        addNew(allActionKeys);
        return true;
    }

    public boolean addNew(List<String> allActionKeys) {
        //添加更新现有的
        List<Record> updateList = new ArrayList<>();
        for (String key : allActionKeys) {
            Action action = JFinal.me().getAction(key, new String[1]);
            if (action == null) {
                continue;
            }
            Record dbPerm = new Record();
            dbPerm.set("perm_id", getUUID32());
            dbPerm.set("perm_name", StrKit.notNull(action.getMethod().getAnnotation(Remark.class)) ? action.getMethod().getAnnotation(Remark.class).value() : null);
            dbPerm.set("action_key", key);
            dbPerm.set("controller", action.getControllerClass().getName());
            dbPerm.set("controller_name", StrKit.notNull(action.getControllerClass().getAnnotation(Remark.class)) ? action.getControllerClass().getAnnotation(Remark.class).value() : null);
            dbPerm.set("create_time", getNowDateTime());
            dbPerm.set("status_flag", "1");
            dbPerm.set("app_id", StrKit.notNull(action.getControllerClass().getAnnotation(APPID.class)) ? action.getControllerClass().getAnnotation(APPID.class).value() : null);
            try {
                permissionDao.save(dbPerm);
            } catch (Exception e) {
                //如果action_key重复异常则执行更新
                String message = e.getMessage();
                if (message.contains("(action_key)") && message.contains("already") && message.contains("exists")) {
                    updateList.add(dbPerm);
                }
            }
        }
        if (updateList.size() > 0) {
            new PermissionThread(updateList).start();
        }
        return true;
    }

    public List<Record> authList(Okv cond) {
        return permissionDao.authList(cond);
    }

}
